package com.woniu.market_stall_web.socket;

import com.google.gson.Gson;

import com.woniu.market_stall_web.mapper.StallMapper;
import com.woniu.market_stall_web.vo.MyMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class PushHandler extends TextWebSocketHandler {
    private Map<String,WebSocketSession> sessionMap = new ConcurrentHashMap<>();


    @Override
    //WebSocketSession  就是一个客户端
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        Map<String, Object> attributes = session.getAttributes();
        System.out.println(attributes);
        String account = (String) attributes.get("account");
        String roomId = (String) attributes.get("roomId");
        System.out.println(roomId);
        //从服务器向客户端发送消息
        MyMsg myMsg = new MyMsg();

        Date sss = new Date();
        //处理时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        String ss=sdf.format(sss);
        DateFormat formate1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat formate2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",Locale.ENGLISH);
        Date date = formate2.parse(ss);
        String dateString = formate1.format(date);
        //把处理好的时间存入  CreateTime
        myMsg.setCreateTime(dateString);

        myMsg.setReciver("all");
        myMsg.setType("all");
        myMsg.setContent(message.getPayload());

        //获取当前用户
        myMsg.setSender(account);
        //把myMsg对象 转成JSON
        Gson gson = new Gson();
        String s = gson.toJson(myMsg);
        TextMessage textMessage = new TextMessage(s);



        //session.sendMessage(message); //向客户端发送信息
        for (Map.Entry<String,WebSocketSession> en : sessionMap.entrySet()){
            //可以判断 客户端  以及  聊天室


            //发送给所有的客户端
            en.getValue().sendMessage(textMessage);

        }

    }

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        System.out.println(session.getId()+"---->连接建立。");



        if (!sessionMap.containsKey(session.getId())){
            sessionMap.put(session.getId(),session);
        }
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        System.out.println(session.getId()+"======>连接关闭.");
        if (sessionMap.containsKey(session.getId())){
            sessionMap.remove(session.getId());
        }
    }

    public int size(){
        return this.sessionMap.size();
    }
}
